package com.zhoutao.blog.seven.api;

import com.zhoutao.blog.seven.model.Response;
import com.zhoutao.blog.seven.model.request.AdminLoginRequest;
import com.zhoutao.blog.seven.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private HttpSession session;

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Response<Boolean> login(@RequestBody AdminLoginRequest request) {
        Long userId = adminService.login(request.getUsername(), request.getPassword());
        session.setAttribute("userId", String.valueOf(userId));
        session.setAttribute("username", "燕归来兮");
        return Response.success(Boolean.TRUE);
    }

    @PostMapping("/logout")
    public Response<Boolean> logout() {
        session.invalidate();
        return Response.success(Boolean.TRUE);
    }


}
